package com.wch.clientdemo.module.base.network;

import java.util.Map;

import com.android.volley.VolleyError;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;


public class VolleyServe {

	/**
	 * 网络请求配置
	 */
	public VolleyOptions moptions = null;
	
	/**
	 * Context请求方
	 */
	private Context context = null;
	
	/**
	 * URL后添加的配置信息
	 */
	private Map<String, String> config = null;
	
	/**
	 * 下载状态： true 正在下载    false 未下载
	 */
	private boolean loading = false;
	
	/**
	 * 网络连接帮助类
	 */
	private NetworkHelper helper = null;
	
	
	
	
	
	/**
	 * 构造方法，初始化相关信息
	 * @param context
	 */
	public VolleyServe(Context context) {
		this.context = context;
		helper = NetworkHelper.getInstance(context);
	}



	public interface Success {
		public void successCallback(Object object, int state);
	}

	public interface Fail {
		public void failCallback(String err);
	}
	
	
	
	public void post(VolleyOptions options,final Success success,final Fail fail){
		
		if(null != options){
			moptions = options;
		}
		
		if(null == options){
			return;
		}
		
		String url = getUrl(moptions);
		if(null != url){
			moptions.setLoading(true);
			loading = true;
			helper.stringPostRequest(url, null, moptions, new NetworkHelper.CallBackString() {
				
				@Override
				public void success(String response) {
					// TODO Auto-generated method stub
					doSuccess(response,success,moptions);
				}
				
				@Override
				public void fail(VolleyError error) {
					// TODO Auto-generated method stub
					doFail(error,fail,moptions);
				}
			}, context);
			
		}
		
	}



	protected void doFail(VolleyError error, Fail fail, VolleyOptions moptions2) {
		// TODO Auto-generated method stub
		setFinishRequest();
		if(null != fail){
			fail.failCallback(String.valueOf(error));
		}
	}



	protected void doSuccess(String response, Success success,
			VolleyOptions moptions2) {
		// TODO Auto-generated method stub
		setFinishRequest();
		if (null == success) {
			return;
		}
		int state = -1;
		Object data = "返回数据包为空";
		
		try {
			//返回数据为空
			if(TextUtils.isEmpty(response) || response.equals("") || response.equals("null")){
				success.successCallback(data, state);
				return;
			}
			
			Map<String, Object> map = null;
			//数据 包解析错误
			if(null ==map || null == map.get("status")){
				data = "返回数据包解析错误";
				success.successCallback(data, state);
				return;
			}
			
			
			
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null != success){
				success.successCallback(data, state);
			}
		}
	}



	/**
	 * 关闭请求
	 */
	private void setFinishRequest() {
		// TODO Auto-generated method stub
		loading =false;
		if(null != moptions){
			moptions.setLoading(false);
		}
	}



	/**
	 * 获取网络请求的URL
	 * @param moptions
	 * @return
	 */
	private String getUrl(VolleyOptions moptions) {
		String preUrl = moptions.getPreUrl();
		String endPoint = moptions.getEndPoint();
		if (null != preUrl && null != endPoint) {
			StringBuilder sb = new StringBuilder();
			sb.append(preUrl);
			sb.append(endPoint);
			//把string字符串变成 URI.BUILDER对象
			Uri.Builder builder = Uri.parse(sb.toString()).buildUpon();
			return builder.build().toString();		
		}
		return null;
	}	
}
